package com.yuan.fmmall.service.Impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yuan.fmmall.dao.ProductImgMapper;
import com.yuan.fmmall.dao.ProductMapper;
import com.yuan.fmmall.dao.ProductParamsMapper;
import com.yuan.fmmall.dao.ProductSkuMapper;
import com.yuan.fmmall.entity.*;
import com.yuan.fmmall.service.ProductService;
import com.yuan.fmmall.vo.ResStatus;
import com.yuan.fmmall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Yuan
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductImgMapper productImgMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductParamsMapper productParamsMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public ResultVO listRecommendProducts() {
        List<ProductVO> productVOS = productMapper.selectRecommendProducts();
        ResultVO resultVO = new ResultVO(ResStatus.OK, "success", productVOS);
        return resultVO;
    }

    @Override
    public ResultVO getProductBasicInfo(String productId) throws JsonProcessingException {
        //根据商品id查询redis数据库
        String productInfo = (String) stringRedisTemplate.boundHashOps("products").get(productId);
        if(productInfo!=null){
            //若在redis中查询到信息，则直接返回
            Product product = objectMapper.readValue(productInfo, Product.class);
            String imgStr = (String) stringRedisTemplate.boundHashOps("productImgs").get(productId);
            JavaType javaType1 = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, ProductImg.class);
            List<ProductImg> productImgList = objectMapper.readValue(imgStr,javaType1);
            String SkuStr = (String) stringRedisTemplate.boundHashOps("productSku").get(productId);
            JavaType javaType2 = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, ProductSku.class);
            List<ProductImg> productSkuList = objectMapper.readValue(SkuStr,javaType2);
            HashMap<String,Object> basicInfo = new HashMap<>();
            basicInfo.put("product",product);
            basicInfo.put("productImgs",productImgList);
            basicInfo.put("productSku",productSkuList);
            return new ResultVO(ResStatus.OK,"success",basicInfo);
        }else {
            //未在redis中查询到信息，则进入数据库中去寻找
            //1.商品基本信息
            Example example = new Example(Product.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("productId",productId);
            criteria.andEqualTo("productStatus",1);//状态为1表示上架商品
            List<Product> products = productMapper.selectByExample(example);
            stringRedisTemplate.boundHashOps("product").put(productId,objectMapper.writeValueAsString(products));

            //2.商品图片
            Example example1 = new Example(ProductImg.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("itemId",productId);
            List<ProductImg> productImgs = productImgMapper.selectByExample(example1);
            stringRedisTemplate.boundHashOps("productImgs").put(productId,objectMapper.writeValueAsString(productImgs));

            //3.商品套餐
            Example example2 = new Example(ProductSku.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("productId",productId);
            criteria2.andEqualTo("status",1);
            List<ProductSku> productSkus = productSkuMapper.selectByExample(example2);
            stringRedisTemplate.boundHashOps("productSku").put(productId,objectMapper.writeValueAsString(productSkus));
            if(products.size()>0){
                //若数据库中存在该数据，则将数据写入redis中
                HashMap<String,Object> basicInfo = new HashMap<>();
                basicInfo.put("product",products);
                basicInfo.put("productImgs",productImgs);
                basicInfo.put("productSku",productSkus);
                return new ResultVO(ResStatus.OK,"success",basicInfo);
            }else {
                return new ResultVO(ResStatus.NO,"fail",null);
            }

        }
    }

    @Override
    public ResultVO getProductParamsById(String productId) {
        //前两句是固定写法
        Example example = new Example(ProductParams.class);
        Example.Criteria criteria = example.createCriteria();
        //表示查询 productId={productId} 的商品
        criteria.andEqualTo("productId",productId);
        //获取查询到的结果
        List<ProductParams> productParams = productParamsMapper.selectByExample(example);
        if(productParams.size()>0){
            return new ResultVO(ResStatus.OK,"success",productParams.get(0));
        }else{
            return new ResultVO(ResStatus.NO,"未查询到该商品的任何参数信息，此商品可能为三无产品",null);
        }
    }
}
